<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Company extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("company_model", "cm");
    }

    function index() {
        $perpage = 10;
        $curpage = $this->uri->segment(3);
        $where = "";
        $data["txtSearch"] = $this->input->post("txtSearch");
        $data["ddPropinsi"] = $this->input->post("ddPropinsi");
        $data["ddKota"] = $this->input->post("ddKota");
        $like = $data["txtSearch"] != "" ? array("company_name" => $data["txtSearch"]):"";
        $data["ddPropinsi"] == "" ? "":$where["province_id"] = $data["ddPropinsi"];
        $data["ddKota"] == "" ? "":$where["city_id"] = $data["ddKota"];
        $data["dt"] = $this->cm->getAll($curpage, $perpage, @$where, @$like, "company_name");
        $this->popuppaging(base_url()."company/index", $this->cm->getAllCount(@$where, @$like), $perpage);
        $this->myview(@$data, "company/index");
    }
    
    function edit(){
        $dt = $this->cm->getById($this->uri->segment(3));
        if ($this->sess->isadmin == 1 || $dt->createby == $this->sess->userid) {
            if ($this->input->post()) {
                $data["low"]->company_name = $this->input->post("txtName");
                $data["low"]->company_address = $this->input->post("txtAddr");
                $data["low"]->company_description = $this->input->post("txtDescr");
                $data["low"]->province_id = $this->input->post("ddPropinsi");
                $data["low"]->city_id = $this->input->post("ddKota");

                $data["low"]->company_name == "" ? @$err->officename = "Nama harus diisi" : "";
                $data["low"]->company_address == "" ? @$err->officeaddress = "Alamat harus diisi" : "";
                $data["low"]->province_id == "" ? @$err->provinceid = "Propinsi harus dipilih" : "";
                $data["low"]->city_id == "" ? @$err->cityid = "Kota harus dipilih" : "";
                if(!@$err){
                    $data["low"]->updatedby = $this->sess->userid;
                    $data["low"]->updateddate = date("Y-m-d H:i:s");
                    $this->cm->update($this->uri->segment(3), $data["low"]);
                    $this->session->set_flashdata("message", "Perusahaan berhasil diubah");
                    $this->session->set_flashdata("redir", base_url() . "company");
                    redirect(base_url() . "alert/success");
                }else{
                    $this->myview(@$data, "company/edit");
                }
            }else{
                $data["low"] = $dt;
                $this->myview(@$data, "company/edit");
            }
        }else{
            $this->session->set_flashdata("message", "Anda tidak mempunyai hak akses untuk merubah data ini.");
            $this->session->set_flashdata("redir", base_url());
            redirect(base_url() . "alert/warning");
        }
    }

    function popup() {
        $perpage = 5;
        $curpage = $this->uri->segment(3);
        $where = "";
        $data["txtSearch"] = $this->input->post("txtSearch");
        $like = $data["txtSearch"] != "" ? array("company_name" => $data["txtSearch"]):"";
        $data["dt"] = $this->cm->getAll($curpage, $perpage, @$where, @$like, "company_name");
        $this->popuppaging(base_url()."company/popup", $this->cm->getAllCount(@$where, @$like), $perpage);
        $this->mypopup("Data Perusahaan", @$data, "company/popup");
    }
    
    function popupadd(){
        if($this->input->post()){
            $dt->txtName = $this->input->post("txtName");
            $dt->txtAddr = $this->input->post("txtAddr");
            $dt->txtDescr = $this->input->post("txtDescr");
            $dt->ddPropinsi = $this->input->post("ddPropinsi");
            $dt->ddKota = $this->input->post("ddKota");
            
            $dt->txtName == "" ? @$err .= "Nama harus diisi\n" : "";
            $dt->txtAddr == "" ? @$err .= "Alamat harus diisi\n" : "";
            $dt->ddPropinsi == "" ? @$err .= "Propinsi harus dipilih\n" : "";
            $dt->ddKota == "" ? @$err .= "Kota harus dipilih\n" : "";
            
            if(@$err != ""){
                $ret->code = "0";
                $ret->err = @$err;
            }else{
                $ret->code = "1";
                $ret->txtName = $this->input->post("txtName");
                $ret->id = $this->om->insert($dt->txtName, $dt->txtAddr, $dt->ddKota, $dt->ddPropinsi, $dt->txtDescr, 2, date('Y-m-d H:i:s'), $this->sess->userid, date('Y-m-d H:i:s'), $this->sess->userid);
            }
            echo json_encode($ret);
        }else{
            $this->mypopup("Form Data Perusahaan", @$data, "company/popupadd");
        }
    }
}